#include "mpLog.h"
#include "mpFileUtils.h"
#include "mpHashUtils.h"
#include "mpCRC64.h"
#include "mpHashUtilsTests.h"
//#include "mpCodecUtils.h"

#include<time.h>




mpHashUtilsTests::mpHashUtilsTests()
{
}

void mpHashUtilsTests::initTestCase(){}
void mpHashUtilsTests::cleanupTestCase(){}

/**
 * @brief 计算文本md5
 */
void mpHashUtilsTests::case_txt_md5(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string text = "一日不见兮，思之如狂。 凤飞翱翔兮，四海求凰。One day's absence makes me think like a maniac. The Phoenix is flying, seeking a mate from all over the world.";
    std::string md5 = "9AE80033A3115A78C06791525B1DAFB6";

    std::string tmp;
    QVERIFY2(MP::HashUtils::text_md5_hex(text,tmp),"exc text_md5");

    //    std::string tmp_md5_bin;
    //    MP::HashUtils::hexStringToBin(tmp,tmp_md5_bin);

    //    std::string tmp_md5_bin_base64;
    //    MP::CodecUtils::base64_decode(tmp_md5_bin,tmp_md5_bin_base64);


    QVERIFY2(md5 == tmp,"equ text_md5");

#endif
}

/**
 * @brief 计算文本sha1
 */
void mpHashUtilsTests::case_txt_sha1(){

#ifdef TEST_STRATEGY_COMPLETED

    std::string text = "一日不见兮，思之如狂。 凤飞翱翔兮，四海求凰。One day's absence makes me think like a maniac. The Phoenix is flying, seeking a mate from all over the world.";
    std::string sha1 = "10B0C36A4D8D195C784CEE281972285FC6B21133";

    std::string tmp;

    QVERIFY2(MP::HashUtils::text_sha1_hex(text,tmp),"exc text_sha1");
    QVERIFY2(sha1 == tmp,"equ text_sha1");

#endif
}

/**
 * @brief 计算文本hmac_sha1
 */
void mpHashUtilsTests::case_txt_hmac_sha1(){

#ifdef TEST_STRATEGY_COMPLETED

    std::string text = "一日不见兮，思之如狂。 凤飞翱翔兮，四海求凰。One day's absence makes me think like a maniac. The Phoenix is flying, seeking a mate from all over the world.";
    std::string key = "mapleleaf";

    std::string hmac_sha1 = "1E10ECC4BCDE33CF99E66601EE920AADB7779984";

    std::string tmp;

    QVERIFY2(MP::HashUtils::text_hmacSha1_hex(text,key,tmp),"exc txt_hmac_sha1");
    QVERIFY2(hmac_sha1 == tmp,"equ txt_hmac_sha1");

#endif
}

/**
 * @brief 计算文本crc64
 */
void mpHashUtilsTests::case_txt_crc64(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string text = "一日不见兮，思之如狂。 凤飞翱翔兮，四海求凰。One day's absence makes me think like a maniac. The Phoenix is flying, seeking a mate from all over the world.";
    uint64_t crc64 = 441185879483591063;

    uint64_t tmp = 0;

    size_t length = text.length();
    char * buffer = const_cast<char *>(text.c_str());
    tmp = MP::CRC64::CalcCRC(tmp, buffer, length);

    QVERIFY2(crc64 == tmp,"equ text_crc64");
#endif
}


/**
 * @brief 计算文件md5
 */
void mpHashUtilsTests::case_file_md5(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string filePath = MP::FileUtils::get_appdir() + PATH_DELIM + "uft8.txt";
    std::string md5 = "9AE80033A3115A78C06791525B1DAFB6";

    std::string tmp;
    QVERIFY2(MP::HashUtils::file_md5_hex(filePath,tmp),"exc file_md5");
    QVERIFY2(md5 == tmp,"equ file_md5");

#endif
}

/**
 * @brief 计算文件sha1
 */
void mpHashUtilsTests::case_file_sha1(){
#ifdef TEST_STRATEGY_COMPLETED

#if defined(PLATFORM_WINDOWS)

    std::string filePath = "F:\\Software\\cn_windows_7_home_premium_with_sp1_x64_dvd_u_676691.iso";
    std::string sha1 = "BB5A8A1480FE54C497601AA1DC7BE698A784BE1C";

    std::string tmp;

    clock_t start,finish;
    double totaltime;
    start = clock();

    QVERIFY2(MP::HashUtils::file_sha1_hex(filePath,tmp),"exc file_sha1");

    finish = clock();
    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;

    MPLOG_INFO << "calculate 3GB+ files use "  << totaltime << " seconds.";

    QVERIFY2(sha1 == tmp,"equ file_sha1");

#endif //PLATFORM_WINDOWS

#endif

}

/**
 * @brief 计算文件crc64
 */
void mpHashUtilsTests::case_file_crc64(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string filePath = MP::FileUtils::get_appdir() + PATH_DELIM + "uft8.txt";
    uint64_t crc64 = 441185879483591063;

    uint64_t tmp = 0;

    std::fstream f(filePath, std::ios::in | std::ios::binary);
    const static int buffer_size = 2048;
    char buffer[buffer_size];
    while (f.good()) {
        f.read(buffer, buffer_size);
        auto bytes_read = f.gcount();
        if (bytes_read > 0) {
            tmp = MP::CRC64::CalcCRC(tmp, static_cast<void*>(buffer),
                                     static_cast<size_t>(bytes_read));
        }
    }
    f.close();


    uint64_t tmp2 = MP::HashUtils::file_crc64(filePath);


    QVERIFY2(crc64 == tmp,"equ file_crc64");
    QVERIFY2(crc64 == tmp2,"equ file_crc64");

    //
    //
    //
    //    uint64_t crc64 = 0;

    //    return crc64;

#endif
}
